/*
 * Copyright (C) 2012 Le Tuan Anh <anh.takaji@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package action;

import com.opensymphony.xwork2.ActionSupport;
import ezcms.dto.Conference;
import ezcms.exceptions.PresentationException;
import ezcms.exceptions.ServiceInvokeException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import service.ConferenceService;
import util.RoleUtil;

/**
 *
 * @author Le Tuan Anh <anh.takaji@gmail.com>
 */
public class HomeAction extends ActionSupport implements ServletRequestAware {

    public static final String WELCOME_GUEST = "welcome_guest";
    public static final String WELCOME_ADMIN = "welcome_admin";
    private HttpServletRequest hsr;
    private List<Conference> conferences;
    private ConferenceService service;

    public HomeAction() throws PresentationException {
        this.service = new ConferenceService();
    }

    @Override
    public String execute() throws PresentationException {
        try {
            setConferences(service.getConferenceDelegate().getActiveConferences());
        } catch (ServiceInvokeException ex) {
            throw new PresentationException(ex);
        }
        if (RoleUtil.hasRole(hsr.getSession(), RoleUtil.ROLE_ADMINISTRATOR)) {
            return WELCOME_ADMIN;
        } else if (RoleUtil.loggedIn(hsr.getSession())) {
            return SUCCESS;
        } else {
            return WELCOME_GUEST;
        }
    }

    /**
     * @return the conferences
     */
    public List<Conference> getConferences() {
        return conferences;
    }

    /**
     * @param conferences the conferences to set
     */
    public void setConferences(List<Conference> conferences) {
        this.conferences = conferences;
    }

    @Override
    public void setServletRequest(HttpServletRequest hsr) {
        this.hsr = hsr;
    }
}
